package com.star.gc4.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.star.gc4.common.R;
import com.star.gc4.entity.Menu;
import com.star.gc4.entity.Message;
import com.star.gc4.entity.Msongs;
import com.star.gc4.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/menu")
public class menuController {

    final Integer admin = 0;   //为管理员

    @Autowired
    private MenuService menuService;



    @PostMapping("/save")
    public R<String> save(HttpServletRequest request,@RequestBody Menu menu){

        Integer userid = 100;
        userid = (Integer) request.getSession().getAttribute("user");
        menu.setUserid(userid);
        menu.setCrdate(LocalDateTime.now());
        menuService.save(menu);
        return R.success("新建成功");
    }

    @GetMapping("/delete")
    public R<String> delete(String[] ids){
        menuService.removeByIds(Arrays.asList(ids));
        return R.success("删除成功");
    }

    @GetMapping("/page")
    public R<Page> page(HttpServletRequest request, int page, int pageSize, @RequestParam(required = false) String label , @RequestParam(required = false)Integer userid){

        Page pageinfo =new Page(page,pageSize);
        LambdaQueryWrapper<Menu> queryWrapper = new LambdaQueryWrapper<>();


        if (label !="" && label != null){
            queryWrapper.eq(Menu::getLabel,label);
        }
        if (userid!=null){
            userid = (Integer) request.getSession().getAttribute("user");
            queryWrapper.eq(Menu::getUserid,userid);
        }else {
            queryWrapper.eq(Menu::getAuthority,admin);
        }
        menuService.page(pageinfo,queryWrapper);
        return R.success(pageinfo);
    }

    /*
    * 获取用户所有歌单
    * */
    @GetMapping("/get")
    public R<List<Menu>> get(HttpServletRequest request){
        Integer userid = 100;
        userid = (Integer) request.getSession().getAttribute("user");

        LambdaQueryWrapper<Menu> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Menu::getUserid,userid);
        List<Menu> list = menuService.list(queryWrapper);
        return R.success(list);
    }

}
